#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using Agresso.Interface.Layer.Service;
using System.Collections;
using System.IO;
using System.Configuration;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class UserControlSoftmar : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
        AlertService m_AlertService;
        string m_startDefault;
       

        public UserControlSoftmar(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            m_current_user = currentUser;
            m_mainForm = mainForm;
            m_startDefault = ConfigurationManager.AppSettings["StartPath"];
            m_AlertService = (AlertService)ServiceFactory.Instance().GetService(ServiceFactory.ALERT_SERVICE);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonExcel.Click += new EventHandler(toolBarButtonExcel_Click);
        }

        void toolBarButtonExcel_Click(object sender, EventArgs e)
        {
            if (listViewAlert.Items.Count == 0)
            {
                MessageBox.Show("No Data to export, please refresh first.");
                return;
            }
            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("Table");
            foreach (ColumnHeader column in listViewAlert.Columns)
            {
                dt.Columns.Add(column.Text);
            }
            foreach (ListViewItem itm in listViewAlert.Items)
            {
                ArrayList row = new ArrayList();
                foreach (ColumnHeader column in listViewAlert.Columns)
                {
                    row.Add(itm.SubItems[column.Index].Text);
                }
                dt.Rows.Add(row.ToArray());
            }

            System.Web.UI.WebControls.DataGrid grid =
                  new System.Web.UI.WebControls.DataGrid();
            grid.HeaderStyle.Font.Bold = true;
            grid.HeaderStyle.Font.Name = "Calibri";
            grid.HeaderStyle.BackColor = Color.Gold;
            grid.Font.Name = "Calibri";
            grid.Font.Size = 9;
            grid.DataSource = ds;
            grid.DataMember = "Table";
            grid.DataBind();
            string filename = "reportSoftmar" + DateTime.Now.Second.ToString() + ".xls";
            string filePath = m_startDefault + filename;
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                using (System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw))
                {
                    grid.RenderControl(hw);
                }
            }
            Link.Open(filename);
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            listViewAlert.Items.Clear();
            IList softmarAlert = m_AlertService.FindAlertByAlertTypeAndStatus(AlertType.SOFTMAR_COMMISSION_INVOICE_TCRELET, AlertStatus.PENDING);
            IList vesselAlert = m_AlertService.FindAlertByAlertTypeAndStatus(AlertType.VESSEL, AlertStatus.PENDING);
            foreach (Alert a in softmarAlert)
            {
                ListViewItem item = listViewAlert.Items.Add(a.alert_modified_date.ToString("dd-MMM-yyyy hh:mm"));
                item.SubItems.Add(a.alert_type.ToString());
                item.SubItems.Add(a.alert_description);
                item.SubItems.Add(a.alert_status.ToString());
                item.Tag = a;
            }
            foreach (Alert a in vesselAlert)
            {
                ListViewItem item = listViewAlert.Items.Add(a.alert_modified_date.ToString("dd-MMM-yyyy hh:mm"));
                item.SubItems.Add(a.alert_type.ToString());
                item.SubItems.Add(a.alert_description);
                item.SubItems.Add(a.alert_status.ToString());
                item.Tag = a;
            }
        }
    }
}